package work.soho.proxy.ui;

import work.soho.proxy.ProxyApplication;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

import static work.soho.proxy.ProxyApplication.infoBox;

public class SystemTrayUi {

    private dorkbox.systemTray.SystemTray systemTray;

    public void setImage(URL image) {
        systemTray.setImage(image);
    }

    public SystemTrayUi(URL image) {
        systemTray = dorkbox.systemTray.SystemTray.get();
        if (systemTray == null) {
            throw new RuntimeException("Unable to load SystemTray!");
        }
        systemTray.installShutdownHook();
        setImage(image);
        systemTray.setStatus("Running");

        //重启代理
        systemTray.getMenu().add(new dorkbox.systemTray.MenuItem("Restart proxy", new ActionListener() {
            @Override
            public
            void actionPerformed(final ActionEvent e) {
                try {
                    ProxyApplication.proxyDaemon.check();
                    infoBox("重启成功！", "阿里代理");
                } catch (Exception checkE) {
                    checkE.printStackTrace();
                }
            }
        }));

        systemTray.getMenu().add(new dorkbox.systemTray.MenuItem("Destroy server", new ActionListener() {
            @Override
            public
            void actionPerformed(final ActionEvent e) {
                try {
                    ProxyApplication.proxyDaemon.dropServer();
                    infoBox("代理服务器删除成功", "阿里代理");
                } catch (Exception deleteException) {
                    deleteException.printStackTrace();
                    //Ignore
                }
            }
        }));

        //配置服务器
        systemTray.getMenu().add(new dorkbox.systemTray.MenuItem("Configure", new ActionListener() {
            @Override
            public
            void actionPerformed(final ActionEvent e) {
                System.out.printf(System.getProperty("user.home"));
                ConfigureFrame.instance().show();
            }
        })); // case does not matter

        //切换服务器
        systemTray.getMenu().add(new dorkbox.systemTray.MenuItem("Switch server", new ActionListener() {
            @Override
            public
            void actionPerformed(final ActionEvent e) {
                try {
                    ProxyApplication.proxyDaemon.switchService();
                    infoBox("重启成功！", "阿里代理");
                } catch (Exception deleteException) {
                    deleteException.printStackTrace();
                    //Ignore
                }
            }
        }));


        systemTray.getMenu().add(new dorkbox.systemTray.MenuItem("Proxy status", new ActionListener() {
            @Override
            public
            void actionPerformed(final ActionEvent e) {
                try {
                    if(ProxyApplication.proxyDaemon.isPortUsed()) {
                        infoBox("代理端口监听正常！", "阿里代理");
                    } else {
                        infoBox("代理状态异常！", "阿里代理");
                    }
                } catch (Exception deleteException) {
                    deleteException.printStackTrace();
                    //Ignore
                }
            }
        }));

        systemTray.getMenu().add(new dorkbox.systemTray.MenuItem("Look ddns ip", new ActionListener() {
            @Override
            public
            void actionPerformed(final ActionEvent e) {
                try {
                    if(!ProxyApplication.proxyDaemon.getConfigManage().getPartsConfigure().getBaseDomain().equals("")) {
                        infoBox("当前内网域名: lan."+ProxyApplication.proxyDaemon.getConfigManage().getPartsConfigure().getBaseDomain() +
                                        " IP:" + ProxyApplication.proxyDaemon.getDdnsLanIp() + "\r\n"
                                        + "当前公网域名：internet."+ProxyApplication.proxyDaemon.getConfigManage().getPartsConfigure().getBaseDomain() +
                                        " IP:" + ProxyApplication.proxyDaemon.getDdnsWanIp()
                                ,"动态域名IP");
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        })); // case does not matter

        systemTray.getMenu().add(new dorkbox.systemTray.MenuItem("Quit", new ActionListener() {
            @Override
            public
            void actionPerformed(final ActionEvent e) {
                systemTray.shutdown();
                System.exit(0);
            }
        })).setShortcut('q'); // case does not matter
    }
}
